package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.enums.UserStatusEnum;
import tech.horgee.group.InsertGroup;
import tech.horgee.group.UpdateGroup;
import tech.horgee.handler.PwdTypeHandler;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Set;

import static tech.horgee.common.MyConstants.SUPER_ADMIN_ID;

/**
 * 用户信息
 *
 * @author haojun
 * @since 2022-06-12
 */
@Getter
@Setter
@TableName(value = "tab_user", autoResultMap = true)
public class User implements Serializable, IWrapper<User> {

    private static final long serialVersionUID = 1L;

    /**
     * 工号
     */
    @NotNull(groups = UpdateGroup.class)
    @Null(groups = InsertGroup.class)
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    /**
     * 密码
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @TableField(typeHandler = PwdTypeHandler.class)
    private String password;

    /**
     * 姓名
     */
    private String nickname;

    /**
     * 身份证
     */
    private String idcard;

    /**
     * 手机
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 岗位ID
     */
    private Long postId;

    /**
     * 直属上级ID
     */
    private Long leaderId;

    /**
     * 状态
     */
    private UserStatusEnum status;

    /**
     * 乐观锁
     */
    @NotNull(groups = UpdateGroup.class)
    @Version
    private Long version;

    /**
     * 逻辑删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 离职时间
     */
    @TableField(updateStrategy = FieldStrategy.ALWAYS)
    private LocalDateTime leaveTime;

    /**
     * 创建时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    /**
     * 角色ID集合
     */
    @TableField(exist = false)
    private Set<Long> roleIds;

    /**
     * 权限集合
     */
    @TableField(exist = false)
    private Set<String> permissions;

    /**
     * 上级
     */
    @TableField(exist = false)
    private String leader;

    /**
     * 部门
     */
    @TableField(exist = false)
    private String dept;

    /**
     * 职位
     */
    @TableField(exist = false)
    private String post;

    @Override
    public LambdaQueryWrapper<User> wrapper() {
        return new LambdaQueryWrapper<User>()
                .like(Objects.nonNull(userId), User::getUserId, userId)
                .eq(Objects.nonNull(password), User::getPassword, password)
                .like(Objects.nonNull(nickname), User::getNickname, nickname)
                .like(Objects.nonNull(idcard), User::getIdcard, idcard)
                .like(Objects.nonNull(phone), User::getPhone, phone)
                .like(Objects.nonNull(email), User::getEmail, email)
                .eq(Objects.nonNull(deptId), User::getDeptId, deptId)
                .eq(Objects.nonNull(postId), User::getPostId, postId)
                .eq(Objects.nonNull(leaderId), User::getLeaderId, leaderId)
                .eq(Objects.nonNull(status), User::getStatus, status)
                .eq(Objects.nonNull(version), User::getVersion, version)
                .eq(Objects.nonNull(deleted), User::getDeleted, deleted)
                .eq(Objects.nonNull(leaveTime), User::getLeaveTime, leaveTime)
                .eq(Objects.nonNull(createTime), User::getCreateTime, createTime)
                .eq(Objects.nonNull(updateTime), User::getUpdateTime, updateTime)
                .ne(User::getUserId, SUPER_ADMIN_ID)
                .orderByDesc(User::getUserId)
                ;
    }
}
